package com.data.basic.chapter05;

/**
 * Created by wangbing on 2018/5/4.
 */

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }

    // 使用arr作为参数，创建一个链表，当前的listNode为链表头节点
    public ListNode (int []arr){
        if (arr==null||arr.length==0){
            throw new IllegalArgumentException("arr can  not be a list");
        }

        this.val = arr[0];
        ListNode cur = this;
        for (int i=0;i<arr.length;i++){
            cur.next = new ListNode(arr[i]);
            cur=cur.next;
        }
    }


    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        ListNode cur = this;
        while(cur.next!=null){
            res.append(cur.val+"-->");
            cur = cur.next;
        }
        res.append("NULL");
        return res.toString();
    }

}
